package com.hdit.guimei.web;

import com.hdit.guimei.domain.Announcement;
import com.hdit.guimei.service.impl.AnnouncementServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.rmi.CORBA.Tie;
import javax.servlet.http.HttpServlet;
import java.util.List;

/**
 * @author 张宝玉
 * @date 2021/1/19 15:45
 */
@Controller
public class AnnouncementController extends HttpServlet {
    @Autowired
    public AnnouncementServiceImpl announcementService;
    /**
     *@methodName  findannouncement
     *@方法功能描述；查询所有的公告
     *@params  [model]
     *@return  java.lang.String
     *@author   张宝玉
     *@Date    2021/1/19 17:45
     */
    @RequestMapping("/findannouncement")
    @ResponseBody
    public List<Announcement> findannouncement(Model model){
        List<Announcement> announcementList = announcementService.findannouncements();
        model.addAttribute("announcementliat",announcementList);
        return announcementList;
    }
    /**
     *@methodName  updateannouncement
     *@方法功能描述：更新公告
     *@params  [id, title, context]
     *@return  java.lang.String
     *@author   张宝玉
     *@Date    2021/1/19 17:45
     */
    @RequestMapping("/updateannouncement")
    @ResponseBody
    public  String updateannouncement(int id,String title,String context){
        int i = announcementService.updateannouncement(id, title, context);
        if (i==1){
            return "OK";
        }else {
            return "false";
        }
    }
    /**
     *@methodName  deleeannoubcement
     *@方法功能描述：通过id删除公告 修改公告状态
     *@params  [id]
     *@return  java.lang.String
     *@author   张宝玉
     *@Date    2021/1/19 17:45
     */
    @RequestMapping("/deleannouncement")
    @ResponseBody
    public String deleeannoubcement(int id){
        int i = announcementService.updateannouncementstate(id);
        if (i==1){
            return "OK";
        }else {
            return "false";
        }
    }
    /**
     *@methodName  addannouncement
     *@方法功能描述：添加公告
     *@params  [id, title, context]
     *@return  java.lang.String
     *@author   张宝玉
     *@Date    2021/1/19 17:47
     */
    @RequestMapping("/addannouncement")
    public  String addannouncement(int id,String title,String context) {
        int i = announcementService.addannouncement(id, title, context);
        if (i==1){
            return "ok";
        }else{
            return  "false";
        }
    }
}
